﻿Imports MyDLL
Imports LinqXmlData
Imports Microsoft.VisualBasic

Public Class Form2

    Dim Nav As New MyDLL.RecordNav
    Dim Contacts As New LinqXmlData.Contacts

    Sub BeginFile()
        Dim C As String = TextBox7.Text

        If CheckBox1.Checked Then
            Form1.RichTextBox1.Text = Form1.RichTextBox1.Text & Contacts.BeginFileXml()
        End If

        If CheckBox2.Checked Then
            Form1.RichTextBox2.Text = Form1.RichTextBox2.Text & Contacts.BeginFileXaml()
        End If
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        BeginFile()
    End Sub

    Sub WriteData(ByVal ID As Integer, ByVal PrevID As String)
        Dim Name As String = TextBox1.Text
        Dim Surname As String = TextBox2.Text
        Dim Email1 As String = TextBox3.Text
        Dim Email2 As String = TextBox4.Text
        Dim Email3 As String = TextBox5.Text
        Dim Title As String = TextBox9.Text
        Dim Tel1 As String = TextBox14.Text
        Dim Tel2 As String = TextBox15.Text

        If CheckBox3.Checked Then
            ID = ID + 1
            TextBox6.Text = ID
        End If

        If CheckBox1.Checked Then
            Form1.RichTextBox1.Text = Form1.RichTextBox1.Text & Contacts.MakeDataXml(ID, PrevID, Name, Surname, Email1, Email2 _
                                       , Email3, Title, Tel1, Tel2).ToString()


        End If

        If CheckBox2.Checked Then

            Form1.RichTextBox2.Text = Form1.RichTextBox2.Text & Contacts.MakeDataXaml(ID, PrevID, Name, Surname, Email1, Email2 _
                                        , Email3, Title, Tel1, Tel2).ToString()

        End If
    End Sub
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

        WriteData(TextBox6.Text, TextBox10.Text)
    End Sub

    Sub EndFile(ByVal IDBox As String)
        Dim Count As String = IDBox

        If CheckBox1.Checked Then
            Form1.RichTextBox1.Text = Form1.RichTextBox1.Text & Contacts.EndFileXml(Count)
        End If

        If CheckBox2.Checked Then
            Form1.RichTextBox2.Text = Form1.RichTextBox2.Text & Contacts.EndFileXaml()
        End If
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        EndFile(TextBox6.Text)
    End Sub

    Sub LoadData()

        Try
            Me.Text = "Email Contacts Database" & " - " & Form1.TextBox3.Text

            Dim ID As String = TextBox6.Text
            Dim Address As String = Form1.TextBox7.Text & Form1.TextBox3.Text & ".xml"

            TextBox9.Text = Contacts.LoadTitle(ID, Address)
            TextBox1.Text = Contacts.LoadName(ID, Address)
            TextBox2.Text = Contacts.LoadSurname(ID, Address)
            TextBox3.Text = Contacts.LoadEmail1(ID, Address)
            TextBox4.Text = Contacts.LoadEmail2(ID, Address)
            TextBox5.Text = Contacts.LoadEmail3(ID, Address)
            TextBox14.Text = Contacts.LoadTel1(ID, Address)
            TextBox15.Text = Contacts.LoadTel2(ID, Address)
            TextBox10.Text = Contacts.LoadPrevID(ID, Address)

            Dim Count As String = Contacts.LoadCount(Address)

            Label7.Text = "There are " & Count & " record(s) in the XML file"
        Catch ex As Exception
            MsgBox("No file has been loaded.")

        End Try

    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click

        LoadData()

    End Sub

    Private Sub Transcribe()
        Try
            Do Until TextBox6.Text = TextBox8.Text           
		        CheckBox3.CheckState = CheckState.Unchecked
                Dim ID As Integer = TextBox6.Text
                ID = ID + 1
                TextBox6.Text = ID
                LoadData()
                WriteData(TextBox6.Text, TextBox10.Text)
            Loop
        Catch ex As Exception
            MsgBox("Choose a proper range.")
        End Try
    End Sub
    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        Transcribe()
    End Sub

    Private Sub ToolStripButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton1.Click
        Nav.GoToFirst(TextBox6)

        LoadData()
    End Sub

    Private Sub ToolStripButton2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton2.Click
        Nav.PrevRec(TextBox6)

        LoadData()
    End Sub

    Private Sub ToolStripButton3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton3.Click
        Nav.NextRec(TextBox6)

        LoadData()
    End Sub

    Private Sub ToolStripButton4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton4.Click
        Nav.GoToLast(TextBox6, Form1.TextBox7.Text & Form1.TextBox3.Text & ".xml")

        LoadData()
    End Sub

    Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
        Form12.Show()
    End Sub

    Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
        Try
            Form13.Show()

            CheckBox3.CheckState = CheckState.Unchecked
            TextBox6.Text = 0


            Dim ID As Integer = TextBox6.Text
            Dim XmlFile As XElement = XElement.Load(Form1.TextBox7.Text & Form1.TextBox3.Text & ".xml")

            Dim Counter As XElement = _
            <Root>
                <%= From el In XmlFile.<Counter>.<Count> _
                    Select el %>
            </Root>
            Dim Count As Integer = Counter

            Dim C As String = TextBox7.Text

            Form13.RichTextBox1.Text = Form13.RichTextBox1.Text & "<Contacts>" & vbCr _
                                      & "<Type>Email Contacts</Type>" & vbCr

            Do Until ID = Count
                ID = ID + 1
                TextBox6.Text = ID

                LoadData()

                Dim Name As String = TextBox1.Text
                Dim Surname As String = TextBox2.Text
                Dim Email1 As String = TextBox3.Text
                Dim Email2 As String = TextBox4.Text
                Dim Email3 As String = TextBox5.Text
                Dim Title As String = TextBox9.Text
                Dim Tel1 As String = TextBox14.Text
                Dim Tel2 As String = TextBox15.Text

                Dim PrevID As String = TextBox10.Text

                Dim Contactxml As XElement = _
           <Contact id=<%= ID %>>
               <PrevID><%= PrevID %></PrevID>
               <Title><%= Title %></Title>
               <Name><%= Name %></Name>
               <Surname><%= Surname %></Surname>
               <Email1><%= Email1 %></Email1>
               <Email2><%= Email2 %></Email2>
               <Email3><%= Email3 %></Email3>
               <Tel1><%= Tel1 %></Tel1>
               <Tel2><%= Tel2 %></Tel2>
           </Contact>

                Form13.RichTextBox1.Text = Form13.RichTextBox1.Text & Contactxml.ToString & vbCr

            Loop

            Dim LastRec As String = TextBox6.Text

            If CheckBox1.Checked Then
                Dim Counter2 As XElement = _
                <Counter>
                    <Count><%= LastRec %></Count>
                </Counter>

                Form13.RichTextBox1.Text = Form13.RichTextBox1.Text & Counter2.ToString & vbCr & "</Contacts>"
            End If

        Catch ex As Exception
            MsgBox("Processing Failed!")
        End Try
    End Sub

    Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click

        Try
            Dim Counter As Integer = TextBox6.Text

            Dim words As String = TextBox12.Text
            Dim split As String() = words.Split(New [Char]() {","c})

            Dim s As String
            For Each s In split
                If s.Trim() <> "" Then
                    TextBox6.Text = s
                    LoadData()
                    Counter = Counter + 1
                    WriteData(Counter, TextBox10.Text)
                End If
            Next s

            TextBox6.Text = Counter

        Catch ex As Exception
            MsgBox("Extraction Failed")
        End Try

    End Sub

    Private Sub Button9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button9.Click
        Try
            Dim Counter As Integer = TextBox6.Text

            Dim words As String = TextBox13.Text
            Dim split As String() = words.Split(New [Char]() {"-"c})

            Dim Init As Integer = split(0) - 1
            Dim Fin As Integer = split(1)

            Do Until Init = Fin
                Init = Init + 1
                TextBox6.Text = Init
                LoadData()
                Counter = Counter + 1
                WriteData(Counter, TextBox10.Text)
            Loop

            TextBox6.Text = Counter

        Catch ex As Exception
            MsgBox("Extraction Failed")
        End Try
    End Sub

    Private Sub ToolStripButton5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton5.Click

        CheckBox3.CheckState = CheckState.Unchecked

        If CheckBox1.Checked Then

            Dim xmlfile As XElement = XElement.Load(Form1.TextBox7.Text & Form1.TextBox3.Text & ".xml")

            Dim count As XElement = <root>
                                        <%= From el In xmlfile.<Counter>.<Count> _
                                            Select el %>
                                    </root>
            Dim lastrec As String = count

            Form1.RichTextBox1.Clear()

            TextBox6.Text = "0"

            LoadData()

            Form1.RichTextBox1.Text = Form1.RichTextBox1.Text & "<Contacts>" & vbCr _
                                          & "<Type>Email Contacts</Type>" & vbCr


            TextBox8.Text = lastrec

            Transcribe()

            TextBox6.Text = CStr(CInt(TextBox6.Text) + 1)
            LoadData()
            WriteData(TextBox6.Text, TextBox10.Text)

            Dim LatestRec As String = TextBox6.Text

            Dim Counter2 As XElement = _
            <Counter>
                <Count><%= LatestRec %></Count>
            </Counter>

            Form1.RichTextBox1.Text = Form1.RichTextBox1.Text & Counter2.ToString & vbCr & "</Contacts>"

            Form1.Save()
            Form1.Open()

            Nav.GoToLast(TextBox6, Form1.TextBox7.Text & Form1.TextBox3.Text & ".xml")

            LoadData()
        End If
    End Sub

    Private Sub ToolStripButton6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton6.Click

        CheckBox3.CheckState = CheckState.Unchecked

        If CheckBox1.Checked Then

            Dim xmlfile As XElement = XElement.Load(Form1.TextBox7.Text & Form1.TextBox3.Text & ".xml")

            Dim count As XElement = <root>
                                        <%= From el In xmlfile.<Counter>.<Count> _
                                            Select el %>
                                    </root>
            Dim lastrec As String = count

            Form1.RichTextBox1.Clear()


            WriteData(TextBox6.Text, TextBox10.Text)
            Dim id As Integer = CInt(TextBox6.Text)


            My.Computer.FileSystem.WriteAllText(My.Computer.FileSystem.SpecialDirectories.MyDocuments & "\XMLO\temp.txt", Form1.RichTextBox1.Text, False)

            Form1.RichTextBox1.Clear()

            TextBox6.Text = "0"

            LoadData()

            Form1.RichTextBox1.Text = Form1.RichTextBox1.Text & "<Contacts>" & vbCr _
                                          & "<Type>Email Contacts</Type>" & vbCr


            TextBox8.Text = CStr(id - 1)

            Transcribe()

            Dim data As String = My.Computer.FileSystem.ReadAllText(My.Computer.FileSystem.SpecialDirectories.MyDocuments & "\XMLO\temp.txt")
            Form1.RichTextBox1.Text = Form1.RichTextBox1.Text & vbCr & data

            TextBox6.Text = CStr(id)
            TextBox8.Text = lastrec

            Transcribe()

            Dim Counter2 As XElement = _
            <Counter>
                <Count><%= lastrec %></Count>
            </Counter>

            Form1.RichTextBox1.Text = Form1.RichTextBox1.Text & Counter2.ToString & vbCr & "</Contacts>"

            Form1.Save()
            Form1.Open()

            TextBox6.Text = CStr(id)
            LoadData()

        End If
    End Sub
End Class